package JavaSE.CODE.javaCollection;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListDemo {
    public static void main(String[] args) {
        //创建
        LinkedList linkedList = new LinkedList<>();
        //添加元素
        Student student = new Student("liudehua",18);
        Student student1 = new Student("guofucheng",18);
        Student student2 = new Student("liangchaowei",18);
        linkedList.add(student);
        linkedList.add(student1);
        linkedList.add(student2);
        linkedList.add(student2);//可以重复添加
        System.out.println(linkedList.size());
        System.out.println(linkedList.toString());
        //删除元素
        linkedList.remove(new Student("liangchaowei",18));
        //因为重写了equals方法，自然可以根据内容删除
        System.out.println(linkedList.size());

        //遍历
        //普通for、增强for略
        //迭代器
        Iterator iterator = linkedList.iterator();
        while(iterator.hasNext()){
            Student s = (Student) iterator.next();//不强转返回的是Object
            System.out.println(s.toString());
        }
        //
        ListIterator listIterator = linkedList.listIterator();
        while(listIterator.hasNext()){
            Object s = listIterator.next();
            System.out.println(s.toString());//但是toString已经被重写了，所以即使不强转也会调用重写的方法，但是不强转，有些Student独有方法用不了
        }

    }
}
